package myJs.user.workflow.featureSelection.dataMatrix

import com.softwaremill.quicklens._
import myJs.Implicits._
import myJs.Utils.layer
import myJs.cps.{PureSelectCp, RSelectMulCp}
import myJs.myPkg.LayerOptions
import myJs.myPkg.reactHookForm._
import myJs.tool.Tool
import myJs.tool.Tool.messages
import shared.Pojo.{DataMatrixData, EachPreprocessData, FsPreprocessData}
import shared.pojo.WorkflowMission.ExtraData
import myJs.user.soft.keggEnrich.KeggEnrich.speciesData

import scala.collection.immutable.SeqMap

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.user.soft.keggEnrich.KeggEnrich

/** Created by yz on 3/11/2020
  */
object DataMatrixCp {

  case class Props(
      dataMatrix: DataMatrixData,
      dataMatrixModifyF: (DataMatrixData => DataMatrixData) => SyncIO[Unit],
      extraData: ExtraData
  ) {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val namePrefix = s"dataMatrix"
    object FTool {

      def groupsModifyF = (v: List[String]) => {
        val newData = modifyLens[DataMatrixData](_.groups).setTo(v)
        props.dataMatrixModifyF(newData)
      }

      def species1ModifyF = (v: String) => {
        val newData = modify(_: DataMatrixData)(_.species1).setTo(v)
        props.dataMatrixModifyF(newData)
      }
      def speciesModifyF = (v: String) => {
        val newData = modify(_: DataMatrixData)(_.species).setTo(v)
        props.dataMatrixModifyF(newData)
      }
      def isSmpModifyF = (v: String) => {
        val newData = modifyLens[DataMatrixData](_.isSmp).setTo(v)
        props.dataMatrixModifyF(newData)
      }

    }

    val formState = ReactHookForm.useFormState(options =
      UseFormStateOptions(
        name = namePrefix
      )
    )

    useEffect(
      SyncIO {
        Tool.refreshNodeColor(
          props.dataMatrix.nodeId,
          formState,
          namePrefix
        )
      },
      List(formState)
    )

    val allGroupsMap = props.extraData.groups.optionMap
    val basicData = props.dataMatrix
    val species1Map = speciesData.keyList.optionMap
    val speciesMap = speciesData(basicData.species1)
    val isSmpMap = if (List("hsa", "mmu").contains(basicData.species)) {
      SeqMap("kegg" -> "KEGG", "smp" -> "SMPDB")
    } else SeqMap("kegg" -> "KEGG")

    div(
      RSelectMulCp
        .Props(
          value = props.dataMatrix.groups,
          label = messages("groups"),
          name = s"${namePrefix}.groups",
          optionMap = allGroupsMap,
          onChange = FTool.groupsModifyF
        )
        .render,
      div(
        className := "form-group col-sm-12",
        label(className := s"", s"${messages("species")}", ":"),
        PureSelectCp
          .Props(
            name = s"${namePrefix}.species1",
            value = basicData.species1,
            onChange = FTool.species1ModifyF,
            optionMap = species1Map
          )
          .render,
        div(
          marginTop := 5.px,
          PureSelectCp
            .Props(
              name = s"${namePrefix}.species",
              value = basicData.species,
              onChange = FTool.speciesModifyF,
              optionMap = speciesMap
            )
            .render
        ),
        div(
          marginTop := 5.px,
          PureSelectCp
            .Props(
              name = s"${namePrefix}.isSmp",
              value = basicData.isSmp,
              onChange = FTool.isSmpModifyF,
              optionMap = isSmpMap
            )
            .render
        )
      )
    )

  }

}
